{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简单的 Numpy Broadcasting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Broadcasting（广播） 解决的是不同形状的矩阵（或者向量）之间的运算问题。\n",
    "\n",
    "在代数运算中，不同形状的矩阵（或者向量）之间无法进行基本运算，但是在Numpy中，只要满足一般规则，这个运算的允许的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 向量和一个数字相加\n",
    "\n",
    "```\n",
    "a = [a1, a2, a3]\n",
    "b\n",
    "\n",
    "c = a + b\n",
    "c = [a1 + b, a2 + b, a3 + b]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1, 2, 3])\n",
    "b = 2\n",
    "a + b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 二维数组和一个数字相加\n",
    "\n",
    "```\n",
    "A = [[a11, a12, a13],\n",
    "     [a21, a22, a23]]\n",
    "b\n",
    "\n",
    "C = A + b\n",
    "C = [[a11 + b, a12 + b, a13 + b],\n",
    "     [a21 + b, a22 + b, a23 + b]]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 5],\n",
       "       [3, 4, 5]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3],\n",
    "             [1, 2, 3]])\n",
    "b = 2\n",
    "C = A + b\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 二维数组和一维数组相加\n",
    "\n",
    "```\n",
    "A = [[a11, a12, a13],\n",
    "     [a21, a22, a23]]\n",
    "b = [b1, b2, b3]\n",
    "\n",
    "C = A + b\n",
    "C = [[a11 + b1, a12 + b2, a13 + b3],\n",
    "     [a21 + b1, a22 + b2, a23 + b3]]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4, 6],\n",
       "       [2, 4, 6]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3],\n",
    "              [1, 2, 3]])\n",
    "b = np.array([1, 2, 3])\n",
    "C = A + b\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Broadcasting的基本原则\n",
    "\n",
    "整体而言，两个不同形状的矩阵（或者向量）进行基本运算，看两个矩阵（或者向量）的倒序维数。如果**倒序维数是一致的**，则“小矩阵”经过复制扩展，和“大矩阵”进行基本运算。\n",
    "\n",
    "比如：\n",
    "\n",
    "```\n",
    "A.shape = (2 x 3)  ->  A.shape = (2 x 3)\n",
    "b.shape = (3)      ->  b.shape = (1 x 3)\n",
    "\n",
    "A.shape = (2 x 3)  ->  A.shape = (2 x 3)\n",
    "b.shape = (1)      ->  b.shape = (1 x 1)\n",
    "```\n",
    "\n",
    "但是，在以下例子中，b无法broadcasting后和A进行运算\n",
    "\n",
    "```\n",
    "A.shape = (2 x 3)\n",
    "b.shape = (1 x 2)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (2,3) (2,) ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-14df91c0db8c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m               [1, 2, 3]])\n\u001b[1;32m      3\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mC\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mA\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      5\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3) (2,) "
     ]
    }
   ],
   "source": [
    "A = np.array([[1, 2, 3],\n",
    "              [1, 2, 3]])\n",
    "b = np.array([1, 2])\n",
    "C = A + b\n",
    "C"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
